/**
 * Proyecto: Cognitum
 * Global IT Services 2008
 * @author Mislas
 */

Cognitum.Aplicacion.PanelList = Ext.extend(Karma.List.AggregateList, {
	initComponent: function() {
        Karma.List.AggregateList.superclass.initComponent.apply(this, arguments);
        this.getStore().reload();
        this.enable();
    },
    openRecord: function(id){
        this.entity.OpenReadOnly(id);
    },
    getTBarControls: function() {
        return [];
    },
    getBbarControls: function(){
        return  this.paginbar = new Karma.Ext.Grid.PagingToolbar({
            store: this.getStore(),
            displayInfo: true,
            plugins: [
                new Ext.ux.grid.AutoRefresher({ refresherTimes: [{
                        text: '',
                        value: 0,
                        timeTextType: 'n'
                    }, {
                        text: '&nbsp;10',
                        value: 10,
                        timeTextType: 's'
                    }, {
                        text: '&nbsp;30',
                        value: 30,
                        timeTextType: 's',
                        checked: true
                    }] }),
                new Ext.ux.grid.PageSizer({
                    initialSize: Karma.Core.Configuration.DefaultPageSize
                })]
        });
    },
    getMenuActions: function() {
        if(!this.submenus) {
            this.submenus = [this.openMenuAction = new Ext.menu.Item({
                iconCls:'icon-window',
                text:'Abrir...',
                scope: this,
                handler:function(){
                    var grid = Ext.getCmp(this.getId() + '-grid');
                    var selected = grid.getSelectionModel().getSelected();
                    this.openRecord(selected.get('Id'));
                }
            })];
        }
        return this.submenus;
    },
    getGridControl: function() {
        var listeners = this.getGridListeners();
        var columns = this.getGridColumns(Ext.isEmpty(this.query) ? null : this.query.Columns);
        var plugins = this.getPlugins(columns);
        var sm = this.getSelectionModel();
        var store = this.getStore();
        store.on('load', this.onLoadRecords, this);
        var grid = {
            xtype: 'grid',
            id: this.getId() + '-grid',
            border: false,
            frame: false,
            columnLines: true,
            hideHeaders: true,
            store: store,
            cm: new Ext.grid.ColumnModel(columns),
            stripeRows: true,
            sm: sm,
            listeners: listeners,
            view: this.getGridView(),
            loadMask: true,
            plugins: plugins
        };
        return grid;
    },
    onLoadRecords: function(store, records) {
        this.setTitle(this.otitle + ' (' + records.length + ')');
    }
});
Ext.reg('pnl.list', Cognitum.Aplicacion.PanelList);

Cognitum.Aplicacion.Mensaje.PanelMensajes = Ext.extend(Ext.TabPanel, {
	
	initComponent: function() {
		this.entity = Karma.Core.ModuleManager.Instance.getEntity('Mensajes');
        this.metadata = this.entity.metadata;
        this.security = this.entity.security;
		
		Ext.apply(this, {
			activeItem: 0,
			anchor: '100%, 30%',
            frame: false,
            border: true,
            defaultType: 'pnl.list',
            defaults: {
                entity: this.entity,
                service: this.metadata.Service,
                metadata: this.metadata,
                security: this.security
            },
			items: []
		});
		Cognitum.Aplicacion.Mensaje.PanelMensajes.superclass.initComponent.apply(this, arguments);
		
        this.entity.useInvoker('GetGrupos', null, {
			fn: function(grupos) {
				Ext.each(grupos, function (grupo) {
					this.add({
						title: grupo.Nombre,
                        otitle: grupo.Nombre,
						grupo: grupo,
						query: grupo.Query
					});
				}, this);
				this.doLayout();
				this.getLayout().setActiveItem(0);
			},
			scope: this
		});
	}
});

Ext.reg('msg.panel', Cognitum.Aplicacion.Mensaje.PanelMensajes);
